package com.zhang.boot.core.service.impl;

//import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.zhang.boot.core.entity.Role;
import com.zhang.boot.core.service.RoleService;
import com.zhang.boot.core.service.TestService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
@Slf4j
public class TestServiceImpl implements TestService {
    private final RoleService roleService;


    @Override
    public void testTransaction() {
        Role role = Role.builder()
                .roleName("TEST1")
                .roleKey("TEST1")
                .orderNum(1)
                .build();
        roleService.save(role);
        //testThrowException();
        TestServiceImpl testService = (TestServiceImpl) AopContext.currentProxy();
        testService.testThrowException();
        throw new RuntimeException("抛了异常");
    }

    @Override
    public void testAsync() {
        log.info("当前线程:" + Thread.currentThread().getName());
        TestServiceImpl testService = (TestServiceImpl) AopContext.currentProxy();
        testService.async();
    }


    @Async
    public void async() {
        log.info("当前线程:" + Thread.currentThread().getName());
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void testThrowException() {
        Role role = Role.builder()
                .roleName("TEST2")
                .roleKey("TEST2")
                .orderNum(2)
                .build();
        roleService.save(role);
    }

    /*@SentinelResource(value = "sayHello")
    public String sayHello(String name) {
        return "Hello" + name;
    }*/


}
